package com.zhihuishu.treenity.controller.operation;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.able.dto.ClassDto;
import com.able.dto.StudentDto;
import com.able.dto.UserSigninDto;
import com.able.openapi.overseas.dto.TeachingTaskDto;
import com.able.openapi.overseas.dto.UserSigninInfoDto;
import com.alibaba.druid.sql.visitor.functions.Substring;
import com.zhihuishu.micro.course.openapi.course.MeetCourseOpenService;
import com.zhihuishu.micro.course.openapi.course.dto.MeetCourseOpenDto;
import com.zhihuishu.remote.RemoteException;
import com.zhihuishu.remote.RemoteResult;
import com.zhihuishu.treenity.consts.Constants;
import com.zhihuishu.treenity.controller.BaseController;
import com.zhihuishu.treenity.controller.BaseController.ResultRequestAndParser;
import com.zhihuishu.treenity.dto.StuInfoDto;
import com.zhihuishu.treenity.log.LoggerCollectionEnum;
import com.zhihuishu.treenity.service.operation.ScoredetailService;
import com.zhihuishu.treenity.util.CopyObjUtils;
import com.zhihuishu.user.openapi.common.UserQueryOpenService;
import com.zhihuishu.user.openapi.common.dto.UserOpenDto;
import com.zhihuishu.user.openapi.wrapper.ResultWrapper;


/***
 * @author User zhouzha
 *
 */
@Controller
@RequestMapping("/operation/scoredetail")
public class ScoredetailController extends BaseController{
	
	@Resource
	private ScoredetailService scoredetailService;
	@Resource
	private MeetCourseOpenService meetCourseOpenService;
	@Resource
	private UserQueryOpenService userQueryOpenService;
	
	@ResponseBody
	@RequestMapping("")
	private ModelAndView scoredetail(Long meetCourseId,Long courseId,Long id,Long recruitId,HttpServletRequest request) throws RemoteException {
		ModelAndView mav = new ModelAndView();
		mav.addObject("courseId", courseId);
		mav.addObject("meetCourseId", meetCourseId);
		mav.addObject("id", id);
		mav.addObject("recruitId", recruitId);
		if(meetCourseId==null||id==null||recruitId==null){
			mav.setViewName("/operation/runhome");
		}else{
			mav.setViewName("/operation/scoredetail");
			
		}
		if(this.isLoginUserByCourseId(mav, courseId, request.getSession())){
			return mav;
		}
		return mav;
	}
	
	@ResponseBody
	@RequestMapping(value = "/upOrSaveStu", method = RequestMethod.POST)
	public RemoteResult<Object> upOrSaveStu(final String upStuInfoString,final String isUp){
				
				RemoteResult<Object> result = new ResultRequestAndParser<Object>("运行-见面课详情",
						"treenity_Scoredetail_upOrSaveStu", "更新或者修改签到打分信息", "upOrSaveStu") {
					@Override
					public RemoteResult<Object> request(Long curUserId) throws RemoteException {
						 List<StuInfoDto> upStuInfos=new ArrayList<StuInfoDto>();
							if (!StringUtils.isEmpty(upStuInfoString)){
								upStuInfos = stringToDtoList(upStuInfoString.contains("[")?upStuInfoString:"["+upStuInfoString+"]",StuInfoDto.class);
							}
						RemoteResult<Object> result = scoredetailService.upOrSaveStu(upStuInfos, isUp);
						return result;
					}
				}.get();
				return result;
				
	}
	@ResponseBody
	@RequestMapping(value = "/findMeetCourseById", method = RequestMethod.POST)
	public Map<Object,Object> findMeetCourseById(Integer id) throws NumberFormatException, RemoteException{
		Map<Object, Object> mapinfo=null;
		try {
			mapinfo = scoredetailService.findMeetCourseById(id);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return mapinfo;
	}
	@ResponseBody
	@RequestMapping(value = "/stuInfo", method = RequestMethod.POST)
	public List<StuInfoDto> stuInfo(Long courseId,Integer recruitId,Integer taskId) throws NumberFormatException, RemoteException{
		List<StuInfoDto> stuInfos=new ArrayList<StuInfoDto>();
		try {
			stuInfos=	scoredetailService.stuInfo(courseId, recruitId, taskId);
		} catch (Exception e) {
			log.error(e.getMessage());
		}
		return stuInfos;
	}
	
	/**
	 * log 存入的表
	 */
	@Override
	protected LoggerCollectionEnum getCollectionEnum(){
        return LoggerCollectionEnum.tblUserSigninCollection;
    }	
	
}